<template>
  <div class="stock-container">
    <div class="stock-chart" ref="chartRef"></div>
  </div>
</template>

<script>
import { mapState } from 'vuex'

export default {
  name: 'Stock',
  data() {
    return {
      chartInstance: null,
      allData: null,
      timer: null,
      currentIndex: 0, // 第一个饼图数据的索引
    }
  },
  computed: {
    ...mapState(['theme'])
  },
  watch: {
    theme() {
      // 主题改变时，销毁当前图表
      this.chartInstance.dispose()
      // 以最新的主题初始化图表
      this.initChart()
      this.screenAdapter()
      this.updateChart()
    }
  },
  created() {
    this.$socket.registerCallBack('stockData', this.getData)
  },
  mounted() {
    this.initChart()
    //this.getData()
    this.$socket.send({
      action: 'getData',
      socketType: 'stockData',
      chartName: 'stock',
      value: ''
     })
    window.addEventListener('resize',this.screenAdapter)
    this.screenAdapter()
  },
  destroyed() {
    window.removeEventListener('resize',this.screenAdapter)
    clearInterval(this.timer)
    this.$socket.unRegisterCallBack('stockData')
  },
  methods: {
    initChart() {
      this.chartInstance = this.$echarts.init(this.$refs.chartRef, this.theme)
      const initOption = {
        title: {
          text: '┃ 库存和销量分析',
          top: 20,
          left: 20
        },
      }
      this.chartInstance.setOption(initOption)
      this.chartInstance.on('mouseout',() => { 
        this.startInterval()
      })
      this.chartInstance.on('mouseover',() => {
        clearInterval(this.timer)
      })
    },

    getData(result) {
      //const result = await this.$http.get('stock')
      this.allData = result
      this.updateChart()
      this.startInterval()
    },

    updateChart() {
      const centerArr = [
        ['18%','40%'],
        ['50%','40%'],
        ['82%','40%'],
        ['34%','75%'],
        ['66%','75%']
      ]
      const colorArr = [
        ['#4FF778','#0BA82C'],
        ['#E5DD45','#E8811C'],
        ['#e8821c','#e55445'],
        ['#5052ee','#ab6ee5'],
        ['#23e5e5','#2e72bf'],
      ]
      const start = this.currentIndex * 5
      const end = (this.currentIndex + 1) * 5
      const showData = this.allData.slice(start, end)
      const seriesArr = showData.map((item,index) => {
        return {
          type: 'pie',
          center: centerArr[index],
          hoverAnimation: false, // 关闭鼠标移入到饼图时的动画
          labelLine: {
            show: false, // 隐藏指示线
          },
          label: {
            position: 'center',
            color: colorArr[index][0]
          },
          data: [
            { 
              name: item.name + '\n\n' + item.sales,
              value: item.sales,
              itemStyle: {
                color: {
                  type: 'linear',   // linear:线性渐变; radial:径向渐变
                  x: 0,
                  y: 1,
                  x2: 0,
                  y2: 0,
                  colorStops: [
                    {
                      offset: 0, color: colorArr[index][0]
                    },
                    {
                      offset: 1, color: colorArr[index][1]
                    }
                  ]
                },
              }
            },
            { 
              value: item.stock,
              itemStyle: {
                color: '#333843'
              }
            },
          ]
        }
      })
      const dataOption = {
        series: seriesArr
      }
      this.chartInstance.setOption(dataOption)
    },

    // 屏幕适配
    screenAdapter() {
      const titleFontSize = this.$refs.chartRef.offsetWidth / 100 * 3.6
      const innerRadius = titleFontSize * 2.8
      const outterRadius = innerRadius * 1.12
      const adapterOption = {
        title: {
          textStyle: {
            fontSize: titleFontSize
          }
        },
        series: [
          {
            type: 'pie',
            radius: [outterRadius, innerRadius],
            label: {
              fontSize: titleFontSize / 2
            }
          },
          {
            type: 'pie',
            radius: [outterRadius, innerRadius],
            label: {
              fontSize: titleFontSize / 2
            }
          },
          {
            type: 'pie',
            radius: [outterRadius, innerRadius],
            label: {
              fontSize: titleFontSize / 2
            }
          },
          {
            type: 'pie',
            radius: [outterRadius, innerRadius],
            label: {
              fontSize: titleFontSize / 2
            }
          },
          {
            type: 'pie',
            radius: [outterRadius, innerRadius],
            label: {
              fontSize: titleFontSize / 2
            }
          },
        ]
      }
      this.chartInstance.setOption(adapterOption)
      this.chartInstance.resize()
    },

    // 定时器
    startInterval() {
      if(this.timer) {
        clearInterval(this.timer)
      }
      this.timer = setInterval(() => {
        this.currentIndex++
        if(this.currentIndex > 1) {
          this.currentIndex = 0
        }
        this.updateChart()
      }, 3000)
    }
  },
}
</script>

<style scoped>

</style>